//
//  MapAnnotation.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/7.
//

import Foundation
import MapKit
import SwiftUI

class CarAnnotationView:MKAnnotationView {
   private var annatationFrame = CGRect(x: -15, y: -24, width: 60, height: 22)
   private let label : UILabel

   override init(annotation:MKAnnotation?,reuseIdentifier:String?) {
      self.label = UILabel(frame: annatationFrame.offsetBy(dx: 0, dy: 0))

      super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)


      self.label.font = UIFont.systemFont(ofSize: 13,weight: .semibold)
      self.label.textColor = .black
      self.label.textAlignment = .center
      self.backgroundColor = .clear


      //self.frame = annatationFrame

      self.addSubview(label)
   }

   func calculateLabelWidth(text: String, font: UIFont) -> CGFloat {
      let textSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)

      let boundingBox = text.boundingRect(with: textSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)

      return ceil(boundingBox.width)
   }

   required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) not implemented!")
   }

   public var carNo : String = "" {
      didSet {
         self.label.text = carNo
         let w = calculateLabelWidth(text: carNo, font: UIFont.systemFont(ofSize: 13,weight: .semibold))
         self.label.frame = CGRect(x: 0-(w/2 - 8), y: -25 , width: w + 10, height: 22)
         self.label.textColor = UIColor(named: "chepaiColor")
         if !carNo.isEmpty {
            self.label.backgroundColor = .yellow // UIColor.init(hex: "#fc9a00ff")
             self.label.backgroundColor = .white
             self.label.layer.borderWidth = 1
             self.label.layer.borderColor = UIColor.black.cgColor
             self.label.layer.cornerRadius = 3
         } else {
            self.label.backgroundColor = .clear
             self.label.layer.borderColor = UIColor.clear.cgColor
         }
      }
   }

   override func draw(_ rect: CGRect) {
      guard let context = UIGraphicsGetCurrentContext() else { return }
      if (carNo == "") {return}

      context.beginPath()
      let carColor = UIColor.init(hex: "#fc9a00ff")
      let pathRect = CGRectInset(annatationFrame, 1, 1)
      let path = UIBezierPath(roundedRect: pathRect, cornerRadius: 10)
      path.lineWidth = 2
      carColor?.setFill()
      UIColor.black.setStroke()
      path.fill()
      path.stroke()
      context.closePath()

   }
}

//class CarAnnotationView: MKAnnotationView {
//  override var annotation: MKAnnotation? { didSet { configureDetailView() } }
//
//
//  required init?(coder aCoder:NSCoder) {
//    super.init(coder: aCoder)
//   configure()
//  }
//
//  override init(annotation:MKAnnotation?, reuseIdentifier:String?) {
//    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
//
//    guard
//      let carAnnotation = self.annotation as? CarAnnotation else {
//      return
//    }
//
//    image = carAnnotation.image
//    configure()
//  }
//}
//
//
//private extension CarAnnotationView {
//  func configure() {
//    canShowCallout = true
//    configureDetailView()
//  }
//
//  func configureDetailView() {
//    print("configureDetailView")
//   // guard let annotation = annotation else { return }
//    let rect = CGRect(origin: .zero, size: CGSize(width: 300, height: 200))
//    let callBtn = UIButton()
//    callBtn.titleLabel!.text = "叫车"
//    callBtn.titleLabel!.textColor = .blue
//    callBtn.titleLabel!.textAlignment = .center
//    callBtn.titleLabel!.numberOfLines = 1
//    callBtn.titleLabel!.frame = CGRect(x:10,y:10,width:100,height:50)
//
//    detailCalloutAccessoryView = callBtn
//
//    NSLayoutConstraint.activate([
//      callBtn.widthAnchor.constraint(equalToConstant: rect.width),
//      callBtn.heightAnchor.constraint(equalToConstant: rect.height)
//    ])
//  }
//}

